package BLL.User;


import BIZ.UserBiz;
import BIZ.statementofncomeBiz;
import javabean.statementOfIncome;
import javabean.userDetails;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet(name = "Session_check", value = "/Session_check")
public class Session_check extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Session_check() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();

        //获取表单递送用户名及密码
        String UserName = new String(request.getParameter("UserName").getBytes(StandardCharsets.UTF_8));
        String Password = new String(request.getParameter("Password").getBytes(StandardCharsets.UTF_8));


        String PhoneNumber;
        String Balance;
        String RegistrationTime;
        String CarNumber1, CarNumber2, CarNumber3;
        String Email;
        String remainingDays = null;
        String remainDegree;
        String Type;

        UserBiz userBiz = new UserBiz();
        userDetails User = userBiz.getUser(UserName, Password);
        userDetails user = userBiz.getuser(UserName, Password);



        PrintWriter out = response.getWriter();
        //进入管理员系统
        if (User != null) {
            session.setAttribute("UserName", UserName); //用户名绑定至session
            session.setAttribute("Password", Password);

            out.println("11");

        }

        //进入用户系统
        if (user != null) {
            session.setAttribute("UserName", UserName); //用户名绑定至session
            session.setAttribute("Password", Password);
            response.sendRedirect("demo.jsp");
            out.println("1");


            //判断用户类型
            Type = userBiz.getusertype(UserName);
            if (Type.equals("0")) {
                //由Mysql调用数据
                PhoneNumber = userBiz.getuserphone(UserName);
                Balance = userBiz.getuserbalance(UserName);
                RegistrationTime = userBiz.getRegdate(UserName);
                CarNumber1 = userBiz.getCarId1(UserName);
                CarNumber2 = userBiz.getCarId2(UserName);
                CarNumber3 = userBiz.getCarId3(UserName);
                Email = userBiz.getEmail(UserName);
                remainingDays = userBiz.getRemainDay(UserName);

                statementofncomeBiz statementofncomeBiz = new statementofncomeBiz();
                long a1;
                String a11;

                //将单个用户的近五次账单记录给session
                List<statementOfIncome> aa = statementofncomeBiz.getuserbill(UserName);
                for (int i = 0; i < aa.size(); i++) {
                    a11 = (aa.get(i).getMoney() + "");
                    session.setAttribute("aa" + i, a11);
                }

                //用户信息绑定至session
                session.setAttribute("Type", "mouth");
                session.setAttribute("PhoneNumber", PhoneNumber);
                session.setAttribute("Balance", Balance);
                session.setAttribute("RegistrationTime", RegistrationTime);
                session.setAttribute("Email", Email);
                session.setAttribute("remainingDays", remainingDays);
                if (CarNumber1 != null) {
                    session.setAttribute("CarNumber1", CarNumber1);
                }
                if (CarNumber2 != null) {
                    session.setAttribute("CarNumber2", CarNumber2);
                }
                if (CarNumber2 != null) {
                    session.setAttribute("CarNumber3", CarNumber3);
                }
            }
            if (Type.equals("1")) {
                //由Mysql调用数据
                PhoneNumber = userBiz.getuserphone(UserName);
                Balance = userBiz.getuserbalance(UserName);
                RegistrationTime = userBiz.getRegdate(UserName);
                CarNumber1 = userBiz.getCarId1(UserName);
                CarNumber2 = userBiz.getCarId2(UserName);
                CarNumber3 = userBiz.getCarId3(UserName);
                Email = userBiz.getEmail(UserName);
                remainDegree = userBiz.getRemainDegree(UserName);

                statementofncomeBiz statementofncomeBiz = new statementofncomeBiz();
                long a1;
                String a11;

                //将单个用户的近五次账单记录给session
                List<statementOfIncome> aa = statementofncomeBiz.getuserbill(UserName);
                for (int i = 0; i < aa.size(); i++) {
                    a11 = (aa.get(i).getMoney() + "");
                    session.setAttribute("aa" + i, a11);
                }
                session.setAttribute("Type", "frequency");
                session.setAttribute("PhoneNumber", PhoneNumber);
                session.setAttribute("Balance", Balance);
                session.setAttribute("RegistrationTime", RegistrationTime);
                session.setAttribute("CarNumber", Email);
                session.setAttribute("remainDegree", remainDegree);

                if (CarNumber1 != null) {
                    session.setAttribute("CarNumber1", CarNumber1);
                }
                if (CarNumber2 != null) {
                    session.setAttribute("CarNumber2", CarNumber2);
                }
                if (CarNumber2 != null) {
                    session.setAttribute("CarNumber3", CarNumber3);
                }

            }
        }

        //未找到账户信息
        if (User == null && user == null) {
            System.out.println("0");

            out.println("'0'");
        }


    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}
